package com.wlz.framework.protocol.dubbo;

import com.wlz.framework.Invocation;
import com.wlz.framework.register.LocalRegister;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;

/**
 * @author wlz
 * @date 2022-07-11  10:16 下午
 */
@Slf4j
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Invocation invocation = (Invocation) msg;

        Class serviceImpl = LocalRegister.get(invocation.getInterfaceName());

        Method method = serviceImpl.getMethod(invocation.getMethodName(), invocation.getParamTypes());

        Object result = method.invoke(serviceImpl.newInstance(), invocation.getParams());

        log.info("netty ---------- , {}", result.toString());

        ctx.writeAndFlush("netty : " + result);
    }
}
